package com.weberror.frontendmonitoring.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cnxym
 * @date 2022-08-13 13:07:33
 * @description 统一数据结果返回
 */
@Data
public class R {

    private Integer status;
    private String msg;
    private Boolean success;
    private Map<String, Object> data = new HashMap<>();

    private R() {

    }

    public static R OK() {
        R r = new R();
        r.setStatus(ResponseCode.SUCCESS);
        r.setMsg("调用成功!");
        r.setSuccess(true);
        return r;
    }

    public static R ERROR() {
        R r = new R();
        r.setStatus(ResponseCode.ERROR);
        r.setMsg("未知的意外错误,请稍后重试!");
        r.setSuccess(false);
        return r;
    }

    public R code(Integer code) {
        this.setStatus(code);
        return this;
    }

    public R message(String message) {
        this.setMsg(message);
        return this;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> data) {
        this.setData(data);
        return this;
    }

}
